<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 百度地图定位 -->
    <script type="text/javascript" id="baiduMapAddress"
        src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=WZp3MbqSXW5YUgDSYIMaqLQHd0tKZeKq"></script>
    <title>百度地图html</title>
    <style type="text/css">
        html,
        body,
        .container {
            display: flex;
            flex-direction: column;
            flex: 1;
        }
    </style>
</head>

<body>
    <div>
        <div class="container" id="container">

        </div>
    </div>

    <script type="text/javascript">
        var map;
        var myGeo;
        var longitude = ''
        var pointArr = [];
        function initData() {
            var that = this
            map = new BMapGL.Map('container');
            //map.centerAndZoom(new BMapGL.Point(116.331398, 39.897445), 16);
            //创建地址解析器实例
            myGeo = new BMapGL.Geocoder();
            //map.enableScrollWheelZoom(true);
            // map.setDisplayOptions({
            // 	poi:true
            // })
            // map.addEventListener("zoomend", function (evt) {
            // 	var offsetPoint = new BMap.Pixel(evt.offsetX, evt.offsetY);    //记录鼠标当前点坐标<br>   alert(offsetPoint.x+","+offsetPointY);
            // });
            map.addEventListener("dragend", function (evt) {
                var center = map.getCenter()
                console.log('经纬度', center)
                var point = evt['latlng']
                var lng = point.lng
                var lat = point.lat
                console.log('拖动的经纬度dragendEvent', point)
                pointArr = []
                if (lng && lat) {
                    longitude = lng
                    latitude = lat
                    var bPoint = new BMapGL.Point(lng, lat);
                    map.centerAndZoom(bPoint, 16);
                    map.panTo(bPoint);
                    //获取详情地址
                    // that.address_detail = obj.formattedAddress
                    //that.baiduInverseGeocoding()
                    //that.aMapSearchNearBy('', pointArr, that.cityName)
                    that.bMapSearchNearBy(that.station_juti_address, that.cityName)
                }
            });
            map.addEventListener("click", function (e) {
                console.log('获取点击的经纬度', e.latlng)
            })
            console.log(this.cityName)
            console.log(this.station_juti_address)
            myGeo.getPoint(this.station_area + this.station_juti_address, function (point) {
                console.log('localPoint', point)
                longitude = point['lng']
                latitude = point['lat']
                bMapSearchNearBy(that.station_juti_address, that.cityName)
                if (point) {
                    //map.centerAndZoom(point, 16);
                } else {
                    vant.Toast('您选择的地址没有解析到结果！');
                }
            }, this.cityName || '北京市')
        }
        function bMapSearchNearBy(keyword) {
            var that = this
            //map.centerAndZoom(new BMap.Point(that.longitude, that.latitude), 16);
            var rendOpts = {
                map: map,
                autoViewport: false,
                // selectFirstResult: false
            };
            var searchOpts = {
                renderOptions: rendOpts,
                //pageCapacity: 8,
                onSearchComplete: function (resultArr) {
                    var resultArr2 = local.getResults()
                    console.log('result2', resultArr2)
                    if (local.getStatus() == BMAP_STATUS_SUCCESS) {
                        that.searchAreaList = []
                        for (var i = 0; i < resultArr.length; i++) {
                            var poiArr = resultArr[i]._pois
                            that.searchAreaList = that.searchAreaList.concat(poiArr)
                        }
                        if (that.searchAreaList.length > 0) {
                            that.searchAreaList.forEach(function (fItem, index) {
                                //计算2点之间的距离
                                var fPoint = fItem['point']
                                fItem['distance'] = that.space(fPoint['lat'], fPoint['lng'], that.latitude, that.longitude)
                            })
                        }
                        // that.initData()
                        // console.log("周围搜索", that.searchAreaList);
                        // that.showPopup = true 
                        // that.vIfMohuShow = true //隐藏顶部

                    } else {
                        //百度地图获取定位失败
                        that.vIfMohuShow = false //隐藏顶部
                    }
                }
            };
            var local = new BMapGL.LocalSearch(map, searchOpts);
            console.log('local', local.searchNearBy)
            //myKeys = ["酒店", "加油站"]
            console.log(that.cityName)
            local.searchNearBy(keyword, that.cityName, {
                lng: that.longitude,
                lat: that.latitude
            })
            // local.searchNearby("山东省青岛市黄岛区大场镇塔山驻地", '青岛市',
            // 	{
            // 		lng: '119.7412571588841',
            // 		lat: '35.72535436269624',

            // 	}
            // );
        }
    </script>
</body>

</html>